package com.example.tianxiaoyu.startproject_128txy.model;

import com.example.tianxiaoyu.startproject_128txy.bean.WeatherResult;
import com.example.tianxiaoyu.startproject_128txy.iface.WeatherIface;

import com.example.tianxiaoyu.startproject_128txy.listener.WeatherListener;
import com.example.tianxiaoyu.startproject_128txy.service.WeatherService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Retrofit;
import retrofit2.Response;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by tianxiaoyu on 2017/3/14.
 */

public class WeatherModel implements WeatherIface{
    private Retrofit retrofit;
    private String BASEURL=
            "http://www/weather.com.cn/";


    public WeatherModel(){
        //Retrofit使用3-1
        retrofit=new Retrofit.Builder()
                .baseUrl(BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    @Override
    public void getWeather(String cityno,final WeatherListener listener ){
    //Retrofit使用3-2
    WeatherService weatherService=retrofit.create(WeatherService.class);
    Call<WeatherResult> call=weatherService.getResult(cityno);
    //Retrofit使用3-3
        call.enqueue(new retrofit2.Callback<WeatherResult>() {
            @Override
            public void onResponse(Call<WeatherResult> call, Response<WeatherResult> response) {
                System.out.println("----WeatherModel-onResponse");
                if(response.isSuccessful() && response.body()!=null)
                    listener.onResponse(response.body().getWeatherInfo());
                else
                    listener.onFail("onresponse fail");
            }

            @Override
            public void onFailure(Call<WeatherResult> call, Throwable t) {
                listener.onFail(t.toString());

            }
        });

        }
}


